home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / answrbok / 6_11.lha / 6_11 / 6_11_out.c < prev    next >
Text File  |  1993-08-08  |  2KB  |  71 lines

  1. * Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
  2. * The C++ Answer Book */
  3. * Tony Hansen */
  4. * All rights reserved. */
  5. *
  6.    Output a type arbint to the given stream.
  7. /
  8. include <arbint.h>
  9. include <debug.h>    // DELETE
  10.  
  11. stream& operator<< (ostream& out, const arbint& j)
  12.  
  13. f (debug&2)    outputarb(cerr, "\n\nout:\nj=", j.p->value, j.p->length);        // DELETE
  14.    arbint val = +j;
  15. f (debug&2)    outputarb(cerr, "val=", val.p->value, val.p->length);    // DELETEd    // reverse the sign of negative numbers
  16.    if (j.isneg())
  17. {
  18. val = -j;
  19. out << "-";
  20. f (debug&2)    outputarb(cerr, "val=", val.p->value, val.p->length);    // DELETE
  21. }
  22.  
  23.    // output can be stored in 6*val.length hyper-decimal
  24.    // digits, each digit holding a value 0..9999.
  25.    const ARB_type dec_base = 10000;
  26. f (debug&2)    cerr << "val.p->length=" << val.p->length << ", ARB_base=" << ARB_base << ", dec_base=" << dec_base << "\n"; // DELETEd    ARB_type *digs =
  27. new ARB_type[val.p->length * ARB_base / dec_base];
  28.    ARB_type *svdigs = digs;
  29.    ARB_type *val_val = val.p->value;
  30.    int val_len = val.p->length;
  31.  
  32.    // store "digits" in reverse order
  33.    for (;;)
  34. {
  35. // *digs++ = val % 10000
  36. // val /= 10000;
  37. ARB_Ltype prevu = 0;
  38.  
  39. for (int r = 0; r < val_len; r++)
  40.     {d        ARB_Ltype tmp = val_val[r] +
  41.     prevu * ARB_base;
  42.     ARB_type tmpq = tmp / dec_base;
  43.     val_val[r] = tmpq;        // % ARB_base
  44.     prevu = tmp - dec_base * tmpq;
  45.     }
  46. *digs++ = prevu;d
  47. bug&2)    cerr << "\tdigit stored: " << prevu << "\n";    // DELETE
  48.  
  49. // if (val == 0)
  50. //    break;d
  51. bug&2)    outputarb(cerr, "val (after div)=", val_val, val_len);    // DELETEd    int allzero = 1;
  52. for (r = 0; r < val_len; r++)
  53.     if (val_val[r] != 0)
  54.         {
  55.     allzero = 0;
  56.     break;
  57.     }
  58.  
  59. if (allzero)
  60.     break;d    }
  61.  
  62.    // Output digits in forward order.
  63.    // All but first digit must be 4 decimal
  64.    // digits in lengths, including leading zeros.
  65.    out << *--digs;
  66.    while (digs-- > svdigs)
  67. out << form("%4.4u", *digs);
  68.    delete svdigs;
  69.    return out;
  70.  
  71.